package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysQuestionRecord;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysCompanyDiagnosis;
import com.ruoyi.system.domain.SysCompanyMember;
import com.ruoyi.system.domain.SysDiagnosisReport;
import com.ruoyi.system.domain.vo.SysDeptReqVo;
import com.ruoyi.system.domain.vo.SysDeptVo;
import com.ruoyi.system.dto.SysDeptDto;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISysCompanyDiagnoseService;
import org.jfree.data.DomainInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * @author lwq
 * @ClassName HomePageServiceImpl.java
 * @Description //TODO 首页数据统计业务$
 * @createTime 2022-06-23 15:55
 */
@Service
public class SysCompanyDiagnoseServiceImpl implements ISysCompanyDiagnoseService {

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SysCompanyMemberMapper sysCompanyMemberMapper;

    @Autowired
    private SysQuestionRecordMapper sysQuestionRecordMapper;

    @Autowired
    private SysCompanyDiagnosisMapper sysCompanyDiagnosisMapper;

    @Autowired
    private SysCompanyFileMapper sysCompanyFileMapper;



    final static String domainInfo="[{\"domainId\":12,\"time\":5},{\"domainId\":13,\"time\":5},{\"domainId\":40,\"time\":10},{\"domainId\":41,\"time\":10},{\"domainId\":42,\"time\":5},{\"domainId\":43,\"time\":10},{\"domainId\":44,\"time\":10},{\"domainId\":24,\"time\":5},{\"domainId\":25,\"time\":5},{\"domainId\":27,\"time\":10},{\"domainId\":29,\"time\":20},{\"domainId\":28,\"time\":20},{\"domainId\":30,\"time\":10},{\"domainId\":32,\"time\":20},{\"domainId\":33,\"time\":5},{\"domainId\":34,\"time\":5},{\"domainId\":71,\"time\":10},{\"domainId\":72,\"time\":5},{\"domainId\":38,\"time\":5},{\"domainId\":39,\"time\":5}]";
    final static String basisInfo="{\"result\":\"0.00\"}";
    /**
     * 查询部门管理数据
     *
     * @param dept 部门信息
     * @return 部门信息集合
     */
    @Override
    public List<SysDeptVo> selectSysDeptReqList(SysDeptReqVo dept)
    {

        List<SysDeptVo> sysDeptVos=deptMapper.selectSysDeptReqList(dept);

        if(CollUtil.isNotEmpty(sysDeptVos)){
            SysCompanyMember sysCompanyMember=new SysCompanyMember();
            Set<Long> deptIds=sysDeptVos.stream().map(sysDeptVo -> {
                return  sysDeptVo.getDeptId();
            }).collect(Collectors.toSet());
            sysCompanyMember.setDeptIds(deptIds);
            sysCompanyMember.setMemberType("1");
            List<SysCompanyMember> sysCompanyMembers=sysCompanyMemberMapper.selectSysCompanyMemberList(sysCompanyMember);
            for (SysDeptVo sysDeptVo:sysDeptVos){
                for (SysCompanyMember sysCompanyMember1:sysCompanyMembers){
                    if(sysCompanyMember1.getDeptId().equals(sysDeptVo.getDeptId())){
                        sysDeptVo.setLeaderId(sysCompanyMember1.getUserId());
                        sysDeptVo.setLeaderName(sysCompanyMember1.getNickName());
                        sysDeptVo.setLeaderPhone(sysCompanyMember1.getPhonenumber());
                    }

                }
            }
        }

        return sysDeptVos;
    }


    /**
     * 企业开启诊断
     *
     * @param
     * @return 结果
     */
    @Override
    public int updateSysDeptDto(SysDeptDto sysDeptDto){
        SysCompanyDiagnosis sysCompanyDiagnosis1=sysCompanyDiagnosisMapper.selectSysCompanyDiagnosisByCompanyId(sysDeptDto.getDeptId());
        if(sysCompanyDiagnosis1!=null){
            return -1;
        }


        SysDept sysDept=new SysDept();
        sysDept.setDeptId(sysDeptDto.getDeptId());
        sysDept.setDiagnoseStatus("1");
        int result= deptMapper.updateDept(sysDept);
        if(result!=0){

            if(CollUtil.isNotEmpty(sysDeptDto.getSysCompanyMembers())){
                for (int i = 0; i <sysDeptDto.getSysCompanyMembers().size() ; i++) {
                    sysDeptDto.getSysCompanyMembers().get(i).setCreateTime(DateUtils.getNowDate());
                }
                sysCompanyMemberMapper.insertSysCompanyMemberList(sysDeptDto.getSysCompanyMembers());
            }

            SysCompanyDiagnosis sysCompanyDiagnosis=new SysCompanyDiagnosis();
            sysCompanyDiagnosis.setDeptId(sysDeptDto.getDeptId());
            sysCompanyDiagnosis.setCreateTime(DateUtils.getNowDate());
            sysCompanyDiagnosis.setCreateBy(getLoginUser().getUsername());
            sysCompanyDiagnosis.setDomainInfo(domainInfo);
            sysCompanyDiagnosis.setBasisInfo(basisInfo);
            sysCompanyDiagnosisMapper.insertSysCompanyDiagnosis(sysCompanyDiagnosis);
        }


        return result;
    }


    @Override
    public int testEnd(Long deptId){
        int result=0;
        SysQuestionRecord sysQuestionRecord=new SysQuestionRecord();
        sysQuestionRecord.setCompanyId(deptId);
        // 制造业中小企业数字化水平评测表 id 102
        sysQuestionRecord.setNaireId(102l);
        List<SysQuestionRecord> sysQuestionRecords= sysQuestionRecordMapper.selectSysQuestionRecordList(sysQuestionRecord);

        if(CollUtil.isNotEmpty(sysQuestionRecords)&&sysQuestionRecords.size()>=26){
            SysDept sysDept=new SysDept();
            sysDept.setDeptId(deptId);
            sysDept.setTestStatus("1");
             result= deptMapper.updateDept(sysDept);
        }else{
            result=-1;
        }
        return result;
    }

    @Override
    public int deleteSysDeptDto(Long deptId){
        SysDept sysDept=new SysDept();
        sysDept.setDeptId(deptId);
        sysDept.setDiagnoseStatus("0");
        sysDept.setTestStatus("0");
        sysDept.setDiagnosePlan("");
        int result= deptMapper.updateDept(sysDept);
        if(result!=0){
            sysCompanyMemberMapper.deleteSysCompanyMemberByDeptId(deptId);
            sysCompanyDiagnosisMapper.deleteSysCompanyDiagnosisByDeptId(deptId);
            sysCompanyFileMapper.deleteSysCompanyFileByDeptId(deptId);

        }

        return result;
    }

}
